package com.station.Activitys.http.manager;

import android.app.Activity;
import android.util.Log;

import com.station.Activitys.bean.News;
import com.station.Activitys.bean.ReleaseNews;
import com.station.Activitys.bean.State;
import com.station.Activitys.http.ApiService;
import com.station.Activitys.http.NetWorkMgr;
import com.station.Activitys.http.api.NewsAPI;
import com.station.Activitys.utils.Constant;

import org.simple.eventbus.EventBus;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 项目名称：
 * 类描述：信息中心 接口管理 manager
 * 创建人：张飞祥
 * 创建时间：2018/5/9 0027 下午 1:33
 * 修改人：张飞祥
 * 修改时间：2018/5/9 0027 下午 1:33
 * 修改备注：
 */
public class NewsManager {

    private static NewsAPI mNewsAPI;
    private static ApiService mApiService;
    private static Activity mActivity;
    private static int mTaskId;

    //加载数据类型
    private int RefType = Constant.FIRST_LOAD;

    private static class NewsApiManagerHolder {
        private static final NewsManager INSTANCE = new NewsManager();
    }
    public static final NewsManager getNewsApiManager(Activity activity, int taskId) {
        mNewsAPI = NetWorkMgr.getInstance(activity)
                .getServiceAPI(NewsAPI.class);
        mApiService = ApiService.getApiService();
        mActivity = activity;
        mTaskId = taskId;
        return NewsApiManagerHolder.INSTANCE;
    }

    public void getType(int type){
        RefType=type;
    }

    /**
     * 信息中心列表
     */
    public void newsList(int UserId,String Title,int pageIndex,int pageSize) {
        Subscription subscription = mNewsAPI
                .newsList(UserId,Title, pageIndex, pageSize).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<News>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_NEWS_LIST_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<News> list) {
                        if(RefType== Constant.FIRST_LOAD){
                            //返回成功 首次加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_NEWS_LIST);
                        }else{
                            //返回成功 上拉加载数据处理
                            EventBus.getDefault().post(list, Constant.BUS_NEWS_LIST_FOOT);
                        }
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 信息中心列表
     */
    public void newsUserList() {
        Subscription subscription = mNewsAPI
                .newsUserList().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ReleaseNews>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_NEWS_RELEASE_LIST_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<ReleaseNews> list) {
                        //返回成功 首次加载数据处理
                        EventBus.getDefault().post(list, Constant.BUS_NEWS_RELEASE_LIST);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 发布信息
     */
    public void newsReleaseSubmit(String Title, String MessageContent,String Reservations,int CreaterId) {
        Subscription subscription = mNewsAPI
                .newsReleaseSubmit(Title, MessageContent, Reservations, CreaterId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<State>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_NEWS_RELEASE_SUBMIT_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(State bean) {
                        //返回成功 首次加载数据处理
                        EventBus.getDefault().post(bean, Constant.BUS_NEWS_RELEASE_SUBMIT);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }


}
